package com.hyper_yang.javase.supplement.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException {
        System.out.println("=====Client=====");
        // 创建客户端
        Socket client = new Socket("192.168.117.1", 3000);
        // 基于客户端获取字节输出流
        OutputStream outputStream = client.getOutputStream();
        PrintStream printStream = new PrintStream(outputStream);
        // 发送数据
        printStream.println("噢噢，这里是客户端，去不去玩啊！！！😀");
        client.shutdownOutput();
        // 基于客户端获取字节输入流
        InputStream inputStream = client.getInputStream();
        byte[] bytes = new byte[1024];
        int len = -1;
        while ((len = inputStream.read(bytes)) != -1) {
            String s = new String(bytes, 0, len);
            System.out.println("服务端发来数据:" + s);
        }
        // 释放资源
        inputStream.close();
        printStream.close();
        outputStream.close();
    }
}
